import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart' ;
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mfkb/event/RouteEvent.dart';
import 'package:mfkb/pages/school/around/MarketDetail.dart';
import 'package:mfkb/pages/school/plugins/SwiperHeadWdget.dart';
import 'package:mfkb/plugin/MyBehavior.dart';
import 'package:mfkb/service/SchoolService.dart';
import 'package:toast/toast.dart';

class HouseDetail extends StatefulWidget {

  HouseDetail(@required this.id) ;

  final int id ;

  @override
  _HouseDetailState createState() => _HouseDetailState();
}

class _HouseDetailState extends State<HouseDetail> {

  dynamic data = null ;

  int _index = 0 ;

  @override
  void initState() {
    super.initState();
    // 请求数据
    getData() ;
  }

  @override
  Widget build(BuildContext context) {
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.light ,
      child: Scaffold(
        body: Container(
          color: Color.fromARGB(255, 246, 245, 248),
          child: ScrollConfiguration(
            behavior: MyBehavior(),
            child: ListView(
              padding: EdgeInsets.all(0),
              children: <Widget>[
                Container(
                  height: ScreenUtil().setWidth(375) * (280 / 375) ,
                  child: SwiperHeadWidget(data: data,) 
                ) ,
                Container(
                  height: ScreenUtil().setHeight(270),
                  decoration: BoxDecoration(
                    color: Colors.white ,
                    borderRadius: BorderRadius.only(bottomLeft: Radius.circular(12) , bottomRight: Radius.circular(12))
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start ,
                    children: <Widget>[
                      Container(
                        height: ScreenUtil().setHeight(50),
                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(15) , right:ScreenUtil().setWidth(56)) ,
                        child: Container(
                          height: ScreenUtil().setHeight(40),
                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(5)),
                          child: Text(data != null ? data["title"] : "加载中..." , overflow: TextOverflow.ellipsis,style: TextStyle(
                            fontSize: ScreenUtil().setSp(16) ,
                            height:ScreenUtil().setHeight(20) / ScreenUtil().setSp(16) ,
                            fontWeight: FontWeight.bold
                          ),maxLines: 2,),
                        ),
                      ) ,
                      Container(
                        height: ScreenUtil().setHeight(40),
                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(15)) ,
                        child: Row(
                          children: <Widget>[
                            Container(
                              height: ScreenUtil().setHeight(20),
                              margin: EdgeInsets.only(top:ScreenUtil().setHeight(8)),
                              child: Text(data == null ? "加载中..." : data["msg"].toString() , style: TextStyle(
                                fontSize: ScreenUtil().setSp(12) ,
                                height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(12) ,
                                color: Color.fromARGB(255, 78, 86, 108)
                              ),),
                            ),
                            Expanded(
                              child: Container(
                                height: ScreenUtil().setHeight(20),
                                margin: EdgeInsets.only(top:ScreenUtil().setHeight(8) , right: ScreenUtil().setWidth(15)),
                                child: Row(
                                  mainAxisAlignment: MainAxisAlignment.end,
                                  children: <Widget>[
                                    Text(data == null ? "加载中..." : data["money"].toString() , style: TextStyle(
                                      fontSize: ScreenUtil().setSp(18) ,
                                      height: ScreenUtil().setHeight(20) / ScreenUtil().setSp(18) ,
                                      color: Colors.red ,
                                      fontWeight: FontWeight.bold
                                    ),textAlign: TextAlign.end,),
                                    Text(data == null ? "加载中..." : data["dw"] == 0 ? "元/日" : "元/月" , style: TextStyle(
                                      fontSize: ScreenUtil().setSp(10) ,
                                      height: ScreenUtil().setHeight(14) / ScreenUtil().setSp(10) ,
                                      color: Colors.red ,
                                      fontWeight: FontWeight.bold
                                    ),textAlign: TextAlign.end,),
                                  ],
                                )
                              ),
                            )
                          ],
                        )
                      ) ,
                      GestureDetector(
                        child: Container(
                          height: ScreenUtil().setHeight(60),
                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(6)),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start ,
                            children: <Widget>[
                              Container(
                                width: ScreenUtil().setWidth(14),
                                height: ScreenUtil().setWidth(14),
                                margin: EdgeInsets.only(left:ScreenUtil().setWidth(15)),
                                child: Image.asset("assets/images/around_where.png" , fit: BoxFit.fill,),
                              ) ,
                              Expanded(
                                child: Container(
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(6)),
                                  child:Text(data == null ? "加载中..." : data["place"] , style: TextStyle(
                                    fontSize: ScreenUtil().setSp(14) ,
                                    fontWeight: FontWeight.w500 ,
                                    fontFamily: "PingFang-SC-Medium,PingFang-SC" ,
                                    color:Color.fromARGB(255, 19, 17, 53)
                                  ),)
                                ),
                              ) ,
                              Container(
                                width: ScreenUtil().setWidth(16),
                                height: ScreenUtil().setHeight(16),
                                margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)),
                                child: Image.asset("assets/images/more_2x.png"),
                              )
                            ],
                          ),
                        ),
                        onTap: (){

                        },
                      ) ,
                      Container(
                        height: ScreenUtil().setHeight(1),
                        color: Color.fromARGB(255, 235, 237, 241),
                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(15) , right:ScreenUtil().setWidth(15)),
                      ) ,
                      GestureDetector(
                        child: Container(
                          height: ScreenUtil().setHeight(56),
                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(0)),
                          padding: EdgeInsets.only(bottom:ScreenUtil().setHeight(4)),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start ,
                            children: <Widget>[
                              Container(
                                width: ScreenUtil().setWidth(14),
                                height: ScreenUtil().setWidth(14),
                                margin: EdgeInsets.only(left:ScreenUtil().setWidth(15)),
                                child: Image.asset("assets/images/around_phone.png" , fit: BoxFit.fill,),
                              ) ,
                              Expanded(
                                child: Container(
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(6)),
                                  child:Text(data == null ? "加载中..." : data["phone"] , style: TextStyle(
                                    fontSize: ScreenUtil().setSp(14) ,
                                    fontWeight: FontWeight.w500 ,
                                    fontFamily: "PingFang-SC-Medium,PingFang-SC" ,
                                    color:Color.fromARGB(255, 19, 17, 53)
                                  ),)
                                ),
                              ) ,
                              Container(
                                width: ScreenUtil().setWidth(16),
                                height: ScreenUtil().setHeight(16),
                                margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)),
                                child: Image.asset("assets/images/more_2x.png"),
                              )
                            ],
                          ),
                        ),
                        onTap: (){
                          if(data != null){
                            eventBus.fire(Phone(data["phone"]));
                          }else{
                            Toast.show("暂时无法打开QQ", context) ;
                          }
                        },
                      ) ,
                      Container(
                        height: ScreenUtil().setHeight(1),
                        color: Color.fromARGB(255, 235, 237, 241),
                        margin: EdgeInsets.only(left:ScreenUtil().setWidth(15) , right:ScreenUtil().setWidth(15)),
                      ) ,
                      GestureDetector(
                        child: Container(
                          height: ScreenUtil().setHeight(52),
                          margin: EdgeInsets.only(top:ScreenUtil().setHeight(0)),
                          child: Row(
                            mainAxisAlignment: MainAxisAlignment.start ,
                            children: <Widget>[
                              Container(
                                width: ScreenUtil().setWidth(14),
                                height: ScreenUtil().setWidth(14),
                                margin: EdgeInsets.only(left:ScreenUtil().setWidth(15)),
                                child: Image.asset("assets/images/qq.png" , fit: BoxFit.fill,),
                              ) ,
                              Expanded(
                                child: Container(
                                  margin: EdgeInsets.only(left:ScreenUtil().setWidth(6)),
                                  child:Text(data == null ? "加载中..." : data["qq"] , style: TextStyle(
                                    fontSize: ScreenUtil().setSp(14) ,
                                    fontWeight: FontWeight.w500 ,
                                    fontFamily: "PingFang-SC-Medium,PingFang-SC" ,
                                    color:Color.fromARGB(255, 19, 17, 53)
                                  ),)
                                ),
                              ) ,
                              Container(
                                width: ScreenUtil().setWidth(16),
                                height: ScreenUtil().setHeight(16),
                                margin: EdgeInsets.only(right:ScreenUtil().setWidth(15)),
                                child: Image.asset("assets/images/more_2x.png"),
                              )
                            ],
                          ),
                        ),
                        onTap: (){
                          if(data != null){
                            eventBus.fire(QQ(data["qq"]));
                          }else{
                            Toast.show("暂时无法打开QQ", context) ;
                          }
                        },
                      ) ,
                    ],
                  ),
                ) ,
                Container(
                  margin: EdgeInsets.only(top:ScreenUtil().setHeight(10)),
                  decoration: BoxDecoration(
                    borderRadius: BorderRadius.only(topLeft:Radius.circular(12) , topRight:Radius.circular(12)) ,
                    color: Colors.white
                  ),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start ,
                    children: <Widget>[
                      Container(
                        height:ScreenUtil().setHeight(42) ,
                        margin: EdgeInsets.only(top:ScreenUtil().setHeight(5) , left:ScreenUtil().setWidth(24)),
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: <Widget>[
                            GestureDetector(
                              child: Container(
                                width: ScreenUtil().setWidth(50),
                                child: Text("介绍" , style: TextStyle(
                                  fontSize: _index == 0 ? ScreenUtil().setSp(16) : ScreenUtil().setSp(14) ,
                                  fontWeight: _index == 0 ? FontWeight.bold : FontWeight.normal ,
                                  color: _index == 0 ? Color.fromARGB(255, 19, 28, 53) : Color.fromARGB(255 , 78, 86, 108) ,
                                ),),
                              ) ,
                              onTap: (){
                                setState(() {
                                  _index = 0 ;
                                });
                              },
                            ) ,
                            GestureDetector(
                              child: Container(
                                margin: EdgeInsets.only(left:ScreenUtil().setWidth(18)),
                                child: Text("图文详情" , style: TextStyle(
                                  fontSize: _index == 1 ? ScreenUtil().setSp(16) : ScreenUtil().setSp(14) ,
                                  fontWeight: _index == 1 ? FontWeight.bold : FontWeight.normal ,
                                  color: _index == 1 ? Color.fromARGB(255, 19, 28, 53) : Color.fromARGB(255 , 78, 86, 108) ,
                                ),),
                              ) ,
                              onTap: (){
                                setState(() {
                                  _index = 1 ;
                                });
                              },
                            ) ,
                          ],
                        ),
                      ),
                      Container(
                        height: ScreenUtil().setHeight(3),
                        width: ScreenUtil().setWidth(24),
                        margin: EdgeInsets.only(left: _index == 0 ? ScreenUtil().setWidth(33) : ScreenUtil().setWidth(112)),
                        decoration: BoxDecoration(
                          color: Color.fromARGB(255, 0, 195, 206)
                        ),
                      ) ,
                      Container(
                        child: _index == 0 ? DescShowWidget(desc:data == null ? "正在加载..." : data["desc_txt"]) : ImageDescWidget(list:data["descImages"]) ,
                      )
                    ],
                  )
                ) ,
              ],
            ),
          )
        ),
      ),
    );
  }

  getData(){
    SchoolService().aroundHouse(widget.id).then((value){
      if(value != null && value["code"] == 200){
        data = value["data"] ;
        setState(() {
          
        });
      }else{
        Toast.show(value == null ? "加载失败" : value["msg"], context) ;
      }
    }) ;
  }

  @override
  void dispose() {
    
    super.dispose();
  }
}